package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProductDamageRpcService;
import com.hunttown.mes.rpc.domain.MesProductDamageDTO;
import com.hunttown.mes.rpc.domain.query.MesProductDamageDTOQuery;
import com.hunttown.mes.service.MesProductDamageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProductDamageConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:33:51
 */
@Service
public class MesProductDamageRpcServiceImpl implements MesProductDamageRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProductDamageRpcServiceImpl.class);

    private final MesProductDamageService mesProductDamageService;

    @Autowired
    public MesProductDamageRpcServiceImpl(MesProductDamageService mesProductDamageService) {
        this.mesProductDamageService = mesProductDamageService;
    }

    @Override
    public MesProductDamageDTO insert(MesProductDamageDTO mesProductDamageDTO) {
        try {
            return convert(mesProductDamageService.insert(convert(mesProductDamageDTO)));
        } catch (Exception e) {
            logger.error("MesProductDamage create failed.", e);
            throw new RuntimeException("MesProductDamage create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProductDamageDTO mesProductDamageDTO) {
        try {
            return mesProductDamageService.updateInfoById(convert(mesProductDamageDTO));
        } catch (Exception e) {
            logger.error("MesProductDamage update by id failed.", e);
            throw new RuntimeException("MesProductDamage update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProductDamageDTOQuery mesProductDamageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProductDamageDTOQuery.getW_id() != null && mesProductDamageDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProductDamageDTOQuery.getW_id());
            }

            if (mesProductDamageDTOQuery.getIds() != null && !mesProductDamageDTOQuery.getIds().equals("")) {
                query.put("ids", mesProductDamageDTOQuery.getIds());
            }

            if (mesProductDamageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProductDamageDTOQuery.getStopFlag());
            }

            if (mesProductDamageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProductDamageDTOQuery.getDeleteFlag());
            }

            return mesProductDamageService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProductDamage update by query failed.", e);
            throw new RuntimeException("MesProductDamage update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProductDamageDTO selectById(Integer id) {
        try {
            return convert(mesProductDamageService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProductDamage select by id failed.", e);
            throw new RuntimeException("MesProductDamage select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProductDamageDTO selectObjByQuery(MesProductDamageDTOQuery mesProductDamageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProductDamageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProductDamageDTOQuery.getStopFlag());
            }

            if (mesProductDamageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProductDamageDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProductDamageDTOQuery.getOrderBy());
            }

            return convert(mesProductDamageService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProductDamage select obj by query failed.", e);
            throw new RuntimeException("MesProductDamage select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProductDamageDTO> getListForPage(MesProductDamageDTOQuery mesProductDamageDTOQuery) {
        Query query = Query.page(mesProductDamageDTOQuery.getPn(), mesProductDamageDTOQuery.getPageSize());

        if (mesProductDamageDTOQuery.getId() != null && mesProductDamageDTOQuery.getId() > 0) {
            query.put("id", mesProductDamageDTOQuery.getId());
        }

        if (mesProductDamageDTOQuery.getIds() != null && !mesProductDamageDTOQuery.getIds().equals("")) {
            query.put("ids", mesProductDamageDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getLikeName())) {
            query.put("likeName", mesProductDamageDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getShowDate())) {
            query.put("showDate", mesProductDamageDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getStartDate())) {
            query.put("startDate", mesProductDamageDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getEndDate())) {
            query.put("endDate", mesProductDamageDTOQuery.getEndDate());
        }

        if (mesProductDamageDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProductDamageDTOQuery.getStopFlag());
        }

        if (mesProductDamageDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProductDamageDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProductDamageDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProductDamageDTOQuery.getOrderBy());
        }

        List<MesProductDamageDTO> list = convert(mesProductDamageService.selectByQuery(query));
        Page<MesProductDamageDTO> page = Page.from(mesProductDamageDTOQuery.getPn(), mesProductDamageDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProductDamageService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProductDamageService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProductDamage delete failed.", e);
            throw new RuntimeException("MesProductDamage delete failed, Please order your code.", e);
        }
    }
}